//
//  SingerInfoViewController.m
//  CatMusic
//
//  Created by dllo on 15/4/27.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "SingerInfoViewController.h"
#import "MusicPlayViewController.h"
#import "BaseTableViewCell.h"
#import "NetWorkingTool.h"
#import "BaseModel.h"
#import "UIImageView+WebCache.h"
#import "SingerTableViewCell.h"
#import "SingerMessageTableViewCell.h"
#import "SingerInfManageViewController.h"
#import "MBProgressHUD.h"
#import "AFNetworkReachabilityManager.h"
#import "AFHTTPRequestOperationManager.h"

#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height


@interface SingerInfoViewController ()<UIScrollViewDelegate>
@property (nonatomic ,retain)NSMutableArray *songArr;
@property (nonatomic , retain)NSMutableArray *specialArr; // 专辑
@property (nonatomic ,retain)NSMutableDictionary *messageDic;  // 信息
@property (nonatomic , retain)NSMutableArray *songListArr;
@property (nonatomic , copy)NSString *tempStr;
@property (nonatomic , retain)NSMutableArray *tempArr;
@property (nonatomic , retain)NSMutableArray *modelSongArr;
@property (nonatomic , retain)NSMutableArray *modelSpecialArr;
@property (nonatomic, retain) MBProgressHUD *mbProView; // 小菊花图片


@property (nonatomic , retain)UIView *myView;  // 动画View
@property (nonatomic , retain)UIImageView *animationImageView; // 动画ImageView


@end

@implementation SingerInfoViewController
- (void)dealloc
{
    [_songArr release];
    [_specialArr release];
    [_messageDic release];
    [_songListArr release];
    [_imageView release];
    [_tempArr release];
    [_modelSongArr release];
    [_modelSpecialArr release];
    [_tableView1 release];
    [_tableView2 release];
    [_tableView3 release];
    [_scrollView release];
    [super dealloc];
}


- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.songArr = [NSMutableArray array];
        self.specialArr = [NSMutableArray array];
        self.messageDic = [NSMutableDictionary dictionary];
        self.tempArr = [NSMutableArray array];
        self.modelSongArr = [NSMutableArray array];
        self.specialArr = [NSMutableArray array];
        self.title = @"歌手";
    }
    return self;
}

- (void)netWorkingToolSongArr
{
    NSString *path1 = [NSString stringWithFormat:@"http://api.dongting.com/song/singer/%@/songs?page=1&size=50" , self.singerId];
    [NetWorkingTool netWorkingToolStr:path1 block:^(id result) {
        NSMutableDictionary *dic1 = result;
        self.songArr = [dic1 objectForKey:@"data"];
        for (NSInteger i = 0; i < self.songArr.count; i++) {
            BaseModel *baseMO = [[BaseModel alloc]init];
            NSMutableDictionary *dic = self.songArr[i];
            baseMO.song_name = dic[@"name"];
            baseMO.singer_name = dic[@"singerName"];
            [self.modelSongArr addObject:baseMO];
            [baseMO release];
        }
        [self.tableView1 reloadData];
    }];
}


//- (void)netWorkingToolSongArr
//{
//    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
//    [manager GET:@"http://api.dongting.com/song/singer/%@/songs?page=1&size=50" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
//        NSMutableDictionary *dic1 = responseObject;
//        self.songArr = [dic1 objectForKey:@"data"];
//        for (NSInteger i = 0; i < self.songArr.count; i++) {
//            BaseModel *baseMO = [[BaseModel alloc]init];
//            NSMutableDictionary *dic = self.songArr[i];
//            baseMO.song_name = dic[@"name"];
//            baseMO.singer_name = dic[@"singerName"];
//            [self.modelSongArr addObject:baseMO];
//            [baseMO release];
//        }
//                [self.tableView1 reloadData];
//    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//        NSLog(@"fail");
//    }];
//}


- (void)netWorkingToolSpecialArr
{
    NSString *path2 = [NSString stringWithFormat:@"http://api.dongting.com/song/singer/%@/albums?page=1" , self.singerId];
    [NetWorkingTool netWorkingToolStr:path2 block:^(id result) {
        NSMutableDictionary *dic2 = result;
        self.specialArr = [dic2 objectForKey:@"data"];
        for (NSInteger i = 0; i < self.specialArr.count; i++) {
            self.tempStr = self.specialArr[i][@"coverId"];
            [self.tempArr addObject:self.tempStr];
        }
        [self.tableView2 reloadData];
    }];
}

- (void)netWorkingToolMessageArr
{
    NSString *paht3 = [NSString stringWithFormat:@"http://api.dongting.com/song/singer/%@?detail=true" , self.singerId];
    [NetWorkingTool netWorkingToolStr:paht3 block:^(id result) {
        NSMutableDictionary *dic3 = result;
        self.messageDic = [dic3 objectForKey:@"data"];
        [self.tableView3 reloadData];
    }];
}
#pragma mark - 小菊花图片的方法
- (void)createMbProView
{
    // 初始化
    self.mbProView = [[[MBProgressHUD alloc] initWithView:self.view] autorelease];
    
    [self.view bringSubviewToFront:self.mbProView];
    
    [_mbProView show:YES];
    _mbProView.labelFont = [UIFont systemFontOfSize:13];
    // 样式 圆形进度条
    //    _mbProView.mode = MBProgressHUDModeCustomView;
    _mbProView.mode = MBProgressHUDModeIndeterminate;
    
    _mbProView.labelText = @"努力加载中~";
    [self.view addSubview:_mbProView];
}



- (void)viewDidLoad {
    [super viewDidLoad];
        // Do any additional setup after loading the view.
    
    // 调用小菊花图片方法
    [self createMbProView];
    self.view.backgroundColor = [UIColor whiteColor];

    
    UIImageView *backgroundImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT / HEIGHT * 554 )];
    [backgroundImageView setImage:[UIImage imageNamed:@"17.jpg"]];
    [self.view addSubview:backgroundImageView];
    
    UIImageView *smallImageView = [[UIImageView alloc]initWithFrame:CGRectMake(self.view.frame.size.width / 2 - 30, HEIGHT / 667 * 80,  60, HEIGHT / 667 * 60)];
    smallImageView.backgroundColor = [UIColor yellowColor];
    NSURL *url = [NSURL URLWithString:self.picStr];
    [smallImageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"1"]];
    [self.view addSubview:smallImageView];
    
    
    self.segmentedControl = [[UISegmentedControl alloc]initWithItems:@[@"单曲" , @"专辑" , @"歌手信息"]];
    self.segmentedControl.frame = CGRectMake(0, HEIGHT / 667 * 200, WIDTH, HEIGHT / 667 * 30);
    [self.segmentedControl addTarget:self action:@selector(segmentedControlAction:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:self.segmentedControl];
    
    
    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, HEIGHT / 667 * 235, WIDTH, HEIGHT / 667 * 432)];
    self.scrollView.contentSize = CGSizeMake(WIDTH * 3, 0);
    self.scrollView.contentOffset = CGPointMake(0, 0);
    [self.scrollView setPagingEnabled:YES];
    [self.view addSubview:self.scrollView];
    self.scrollView.delegate = self;
    
    
    
    self.tableView1 = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT / 667 * 320) style:UITableViewStylePlain];
    self.tableView1.dataSource =self;
    self.tableView1.delegate =self;
    self.tableView1.backgroundColor = [UIColor clearColor];
    [self.scrollView addSubview:self.tableView1];
    
    
    self.tableView2 = [[UITableView alloc]initWithFrame:CGRectMake(WIDTH, 0, WIDTH, HEIGHT / 667 * 325) style:UITableViewStylePlain];
    self.tableView2.delegate = self;
    self.tableView2.dataSource =self;
    self.tableView2.rowHeight = 70;
    [self.tableView2 setBackgroundColor:[UIColor clearColor]];
    [self.scrollView addSubview:self.tableView2];

    
    
    self.tableView3 = [[UITableView alloc]initWithFrame:CGRectMake(WIDTH * 2, 0, WIDTH, HEIGHT / 667 * 383)style:UITableViewStylePlain];
    self.tableView3.delegate = self;
    self.tableView3.dataSource = self;
    [self.tableView3 setBackgroundColor:[UIColor clearColor]];
    self.tableView3.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.scrollView addSubview:self.tableView3];
    
    
//    [self netWorkingToolSongArr];
//    [self netWorkingToolSpecialArr];
//    [self netWorkingToolMessageArr];

    
    
    [_scrollView release];
    [_tableView1 release];
    [_tableView2 release];
    [_tableView3 release];
    [_imageView release];
    [smallImageView release];
    [_segmentedControl release];
    [backgroundImageView release];
    
    
    
    
    
    
#pragma mark - 判断网络状态    AFN
    AFNetworkReachabilityManager *mgr = [AFNetworkReachabilityManager sharedManager];
    
    // 2.设置网络状态改变后的处理
    [mgr setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        // 当网络状态改变了, 就会调用这个block
        switch (status) {
            case AFNetworkReachabilityStatusUnknown: // 未知网络
                NSLog(@"未知网络");
                break;
                
            case AFNetworkReachabilityStatusNotReachable: // 没有网络(断网)
                NSLog(@"没有网络(断网)");
                [self alertViewCancel];
                break;
                
            case AFNetworkReachabilityStatusReachableViaWWAN: // 手机自带网络
                NSLog(@"手机自带网络");
                [self alertViewCancelAFNetworkReachabilityStatusReachableViaWWAN];
                break;
                
            case AFNetworkReachabilityStatusReachableViaWiFi: // WIFI
                NSLog(@"WIFI");
                [self netWorkingToolSongArr];
                [self netWorkingToolSpecialArr];
                [self netWorkingToolMessageArr];
                break;
        }
    }];
    
    // 3.开始监控
    [mgr startMonitoring];
    [self animationImage];
}

- (void)animationImage
{
    self.myView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT / 667 * 320)];
    self.myView.backgroundColor = [UIColor whiteColor];
//    self.myView.alpha = 0.2;
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(WIDTH / 2 - 40, HEIGHT / 667 * 20, WIDTH / 375 * 160, HEIGHT / 667 * 40)];
    label.text = @"努力加载中....";
    [self.myView addSubview:label];
    [label release];
    [self.tableView1 addSubview:self.myView];
    self.animationImageView = [[UIImageView alloc]initWithFrame:CGRectMake(WIDTH / 2 -80, HEIGHT / 667 * 80, WIDTH / 375 * 160, HEIGHT / 667 * 160)];
//    self.animationImageView.backgroundColor = [UIColor clearColor];
    [self.myView addSubview:self.animationImageView];
    NSMutableArray *imageArr = [NSMutableArray array];
    for (NSInteger i = 1; i < 56 ; i++) {
        NSString *imageName = [NSString stringWithFormat:@"a%ld.png" , i];
        UIImage *image = [UIImage imageNamed:imageName];
        [imageArr addObject:image];
    }
    self.animationImageView.animationImages = imageArr;
    self.animationImageView.animationDuration = 2.5;
    [self.animationImageView startAnimating];
    [_animationImageView release];
}


- (void)alertViewCancel
{
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"网络断开连接" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:nil];
    [alert show];
}

- (void)alertViewCancelAFNetworkReachabilityStatusReachableViaWWAN
{
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"手机自带网络" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:nil];
    [alert show];
}


- (void)segmentedControlAction:(UISegmentedControl *)segment
{
    if (segment.selectedSegmentIndex == 0) {
        self.scrollView.contentOffset = CGPointMake(0, 0);
    }else if (segment.selectedSegmentIndex == 1){
        self.scrollView.contentOffset = CGPointMake(WIDTH, 0);
    }else if (segment.selectedSegmentIndex == 2){   
        self.scrollView.contentOffset = CGPointMake(WIDTH * 2, 0);
    }
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    NSInteger dx = self.scrollView.contentOffset.x;
    NSInteger pageNum = dx / self.view.frame.size.width;
    if (pageNum == 0 ) {
        self.segmentedControl.selectedSegmentIndex = 0;
    }
    else if(pageNum == 1)
    {
        self.segmentedControl.selectedSegmentIndex = 1;
    }
    else
    {
        self.segmentedControl.selectedSegmentIndex = 2;
    }
}




- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.tableView1 == tableView) {
        return self.songArr.count;
    }
    else if(tableView == self.tableView2)
    {
        return self.specialArr.count;
    }
    else
    
        return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.tableView1) {
        static NSString *reuse1 = @"reuse";
        BaseTableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:reuse1];
        if (cell1 == nil) {
            cell1 = [[[BaseTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse1] autorelease];
        }
        cell1.backgroundColor = [UIColor clearColor];
        cell1.BaseSongNameLabel.text = self.songArr[indexPath.row][@"name"];
        cell1.BaseSingerNameLabel.text = self.songArr[indexPath.row][@"singerName"];
        cell1.baseNumberLabel.text = [NSString stringWithFormat:@"%ld" , indexPath.row + 1];
        [self.myView removeFromSuperview];
        return cell1;
    }
    else if (tableView == self.tableView2)
    {
        static NSString *reuse2 = @"reuse1";
        SingerTableViewCell *cell2 = [tableView dequeueReusableCellWithIdentifier:reuse2];
        if (cell2 == nil) {
            cell2 = [[[SingerTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse2] autorelease];
        }
        cell2.backgroundColor = [UIColor clearColor];
        cell2.songLabel.text = self.specialArr[indexPath.row][@"name"];
        cell2.companyNameLabel.text = self.specialArr[indexPath.row][@"companyName"];
        NSURL *url = [NSURL URLWithString:self.specialArr[indexPath.row][@"picUrl"]];
        [cell2.PicImageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"placeHolder"]];
        return cell2;
    }
    else
    {
        static NSString *reuse3 = @"reuse";
        
        SingerMessageTableViewCell *cell3 = [tableView dequeueReusableCellWithIdentifier:reuse3];
        if (cell3 == nil) {
            cell3 = [[[SingerMessageTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse3] autorelease];
        }
        cell3.contentView.backgroundColor = [UIColor clearColor];
        cell3.backgroundColor = [UIColor clearColor];
        cell3.singerMassageLabel.text = [self.messageDic objectForKey:@"brief"];
        return cell3;
    }
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (tableView == self.tableView1) {
        MusicPlayViewController *musicPlayVC1 = [[MusicPlayViewController alloc]init];
        musicPlayVC1.modelArr = self.modelSongArr;
        musicPlayVC1.indexPath = indexPath.row;
        [self.navigationController pushViewController:musicPlayVC1 animated:YES];
        [musicPlayVC1 release];
    }else if (tableView == self.tableView2){
        SingerInfManageViewController *singerInfVC = [[SingerInfManageViewController alloc]init];
        singerInfVC.picStr = self.picStr;
        singerInfVC.number = self.tempArr[indexPath.row];
        [self.navigationController pushViewController:singerInfVC animated:YES];
        [singerInfVC release];
    }else
    {
        NSLog(@"成功!!!!");
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.tableView1) {
        return 70;
    }else if (tableView == self.tableView2){
        return 70;
    }else{
        NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:18] , NSFontAttributeName, nil];
        CGRect rect = [[self.messageDic objectForKey:@"brief"] boundingRectWithSize:CGSizeMake(335, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
        return rect.size.height + 200;
    }
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
